<?php
namespace Components\Account\User\Wallet;

class Filter extends \Phalcon\Filter implements \Phalcon\FilterInterface {
	private $data = [];
	private $errors = [];

	public function setData($data) {
		$this -> data = $data;
		return $this;
	}

	public function execute() {
		if(empty($this -> errors)) {
			return ['code'=>0,'data'=>$this->data];
		}

		return ['code'=>-1,'message'=>$this->errors[0]];
	}

	public function addType() {
		if(isset($this -> data['type']) && !empty($this -> data['type'])) {
			$this -> data['type'] = $this -> sanitize($this -> data['type'], 'int');
		}

		return $this;
	}

	public function addAccount() {
		if(isset($this -> data['account']) && !empty($this -> data['account'])) {
			$this -> data['account'] = $this -> sanitize($this -> data['account'], ['string','trim']);
		}

		$findFirst = \Components\Account\User\Model::findFirst(['account = "' . $this -> data['account'] . '"']);
		if(!$findFirst) {
			$this -> errors[] = '用户不存在';
		} else {
			$this -> data['user_id'] = $findFirst -> user_id;
			unset($this -> data['account']);
		}

		return $this;
	}

	public function addAmount() {
		if(isset($this -> data['amount']) && !empty($this -> data['amount'])) {
			$this -> data['amount'] = $this -> sanitize($this -> data['amount'], 'float');
			$this -> data['amount'] = abs($this -> data['amount']);
		}

		if($this -> data['amount'] == 0) {
			$this -> errors[] = '充值数量不能等于0';
		}

		return $this;
	}

	public function addActive() {
		if(isset($this -> data['active']) && !empty($this -> data['active'])) {
			$this -> data['active'] = $this -> sanitize($this -> data['active'], 'int');
		}

		return $this;
	}
}
